# -*- coding:utf-8 -*-
import turtle
import math
import random


class Regular_Star_Polygon(turtle.Turtle):
    '''画正星形、多角形的类;运行本类需要导入math模块'''
    def __init__(self,radius,number,cpoint=[0,0]):
        '''r 半径 n 角数 '''
        super(Regular_Star_Polygon,self).__init__()  #关键 显示调用父类已经定义的__init__
        self.radius = radius
        self.number = number
        if number < 3:
            self.number =int(input("多角形数量参数不合理，请重新输入大于等于3的整数："))
        self.cpoint = cpoint
        self.pu()
        self.goto(cpoint[0],cpoint[1])
        self.pd() 
        
         
        
    @property  #属性
    def Interior_angle(self):
        '''内角'''
        inner_angle = (180 * (self.number -2))/self.number
        return inner_angle

    @property
    def Exterior_angle(self):
        '''
        外角
        '''
        E_a = 360 / self.number
        if self.number == 4:
            E_a = 45
        return E_a

    @property
    def Edge_Length(self):
        '''正多边形的边长 '''
        #E_l = math.sqrt(2 * math.pow(self.radius,2) - 2 * self.radius * self.radius * math.cos(math.radians(360/self.number)))
        E_l =2 * self.radius * math.sin(math.radians(180/self.number))
                #两个公式的结果是一样的
        return E_l

    @property
    def Angle_length(self):
        ''' 多角星角的边长'''
        A_l = self.Edge_Length / (
            2 * math.cos
            (
                math.radians
                (
                    (180 - self.Interior_angle
                     )/2
                    )
                )
            )
        if self.number == 4:
            A_l = self.Edge_Length / (2 * math.cos
                                      (math.radians
                                       (
                                           (180 - 135)/2)))
        return A_l

     
    @property
    def coordinate(self,w = 2):
        '''获取当前坐标,默认精度2位'''
        return (round(self.xcor(),w),round(self.ycor(),w))
       
    
    def set_new_point(self,x,y,angle = 0):
        self.cpoint = [x,y]
        self.my_go(x,y)
        self.setheading(angle)
    
    def my_go(self,x,y):
        '''移动到坐标'''
        self.pu()
        self.goto(x,y)
        self.pd() 
    
    def write_coordinate(self,color = 'crimson'):
        '''#当前位置标出坐标点'''
        self.write('({0},{1})'.format(round(self.xcor(),2),round(self.ycor(),2)))
        self.dot(3,color)
    
    def draw_polygon(self):
        '''画出多边形状'''
        if self.number >= 3:
            for _ in range(self.number):
                self.fd(self.Edge_Length)
                self.lt(360/self.number)
    
    def draw_star_polygon(self,p_color = 'black',color='', mode = 0,ro = 0):
        '''绘制多角星，输入颜色会进行填充操作
        mode:0为默认围绕当前点为中心绘制，1为从当前点直接绘制'''
        self.pencolor(p_color)
        if  len(color) > 0:
            self.pencolor(color)
            self.fillcolor(color)
            self.begin_fill()
        #在中心点模式下，确定绘制起点   
        if mode == 0:
            #起始点的角度，确保绘制出来的图形正面朝上
            rotation_angle =270-self.Interior_angle/2+ 90-self.Exterior_angle
            if self.number == 4:
                rotation_angle = 222.5
            #print('{0}角星旋转角度：{1}'.format(self.number,rotation_angle))
            self.seth(rotation_angle + ro)
            self.pu()
            self.fd(self.radius)
            self.pd()
            self.seth(ro)
        
        self.lt(self.Exterior_angle/2)
        self.fd(self.Angle_length)
        
        for _ in range(self.number):
            self.rt(self.Exterior_angle)
            self.fd(self.Angle_length)
            if self.number == 4:
                self.lt(self.Exterior_angle*3)
            else:
                self.lt(self.Exterior_angle*2)
            #self.rt(self.Exterior_angle)
            self.fd(self.Angle_length)
        if len(color) > 0:
            self.end_fill()   
         
    def draw_angle_line(self):
        for z in range(0,360,int(360/self.number)):
            self.seth(z)
            self.fd(self.radius)
            self.write(z)
            self.bk(self.radius)
        
    def print_self(self):
        '''输入自身相关'''
        print('多角星\n半径：{0}\n' \
              '角星的边长:{1}\n' \
              '多边边长：{2}\n' \
              '内角：{3}\n' \
              '外角：{4}\n' \
              '角数量：{5}'.format(self.radius,
                               self.Angle_length,
                               self.Edge_Length,
                               self.Interior_angle,
                               self.Exterior_angle,
                               self.number) )
        print('当前坐标：{0}'.format(self.coordinate))
        
    def cls_test(self):
        
        self.set_new_point(-200,0)
        self.write_coordinate()
        self.draw_star_polygon(mode=1)
        self.draw_polygon()
        self.draw_star_polygon()
        self.print_self()
        
        self.set_new_point(-0,0)
        self.write_coordinate()
        self.draw_star_polygon()
        #self.draw_polygon()
        self.print_self()
        
        
        self.set_new_point(200,0)
        self.write_coordinate()
        self.draw_star_polygon()
        self.draw_polygon()
        self.print_self()



def get_color():
        '''
        随机获取RGB模式下的一个颜色
        '''
        rgb = []
        for i in range(3):
            rgb.append(random.randint(0,255))
        return rgb

def cls_testv1():
    x = 2
    nn = Regular_Star_Polygon(10,3)
    for i in range(100,450,50):
        x +=1
        nn.radius = i
        nn.number = x
        nn.set_new_point(0,0)
        nn.draw_star_polygon(mode = 0)
    
def cls_testn4():
    nn = Regular_Star_Polygon(100,4)
    nn.print_self()
    for i in range (215,229,1):
        nn.radius += 20
        nn.set_new_point(0,0)
        nn.screen.colormode(255)
        nn.draw_star_polygon(p_color = get_color(), mode = 0,ro = i + 10) #ro i+10、i*10，没有效果完全不一样
    
cls_testn4()
#n5= Regular_Star_Polygon(100,1)           
#n5= Regular_Star_Polygon(100,3)        
#n5.cls_test()
#n5= Regular_Star_Polygon(100,4)
#n5.cls_test()
#n5= Regular_Star_Polygon(100,5)
#n5.cls_test()